package com.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.study.command.UserCommand;
import com.study.dao.entity.MovieUser;

import rx.Observable;


@RestController
public class MovieUserController {
	
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	LoadBalancerClient LoadBalancerClient;
	
	@Autowired
	UserCommand userCommand;
	
//	@Autowired
//	UserObservableCommand userObservableCommand;
	
	@GetMapping("/ribbon/{id}")
	@HystrixCommand(fallbackMethod = "selectByIdFallback")
	public MovieUser selectById(@PathVariable("id") int id) {
		//spring.application.name=sunxj-microservice-provider-user
		//vip=virtual ip
		MovieUser movieUser = restTemplate.getForObject("http://sunxj-microservice-provider-user/simple/" + id, MovieUser.class);
		return movieUser;
	}
	
	//只能调用一次
	@GetMapping("/ribbon/userCommand")
	public MovieUser userCommand() {
		MovieUser movieUser = userCommand.execute();
		System.out.println("*************: " + movieUser);
		return movieUser;
	}
	
//	@GetMapping("/ribbon/userCommand2")
//	public MovieUser userCommand2() {
//		UserCommand userCommand2 = new UserCommand();
//		MovieUser user = userCommand2.execute();
//		System.out.println("*************: " + user);
//		return user;
//	}
	
//	@GetMapping("/ribbon/userObservableCommand")
//	public String userObservableCommand() {
//		Observable<MovieUser> observable = userObservableCommand.observe();
//		observable.su
//		
//	}
	
	public MovieUser selectByIdFallback(int id) {
		//spring.application.name=sunxj-microservice-provider-user
		//vip=virtual ip
		MovieUser movieUser = new MovieUser();
		movieUser.setId(0);
		return movieUser;
	}

}
